/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.entity.FileCate;
import cn.webrx.wxs.mapper.FileCateMapper;
import cn.webrx.wxs.vo.VoFileCate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: wxshop - AdminFileCateController
 * <p>Powered by webrx On 2021-04-25 19:00:02
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller
@RequestMapping("/admin")
public class AdminFileCateController {
    @Autowired
    private FileCateMapper fcdao;

    @GetMapping("fileCate/lists")
    @ResponseBody
    public List<VoFileCate> lists() {
        VoFileCate vfc = new VoFileCate();
        vfc.setId(0);
        vfc.setTitle("全部");
        List<VoFileCate> list = fcdao.queryCate();
        list.add(0, vfc);
        return list;
    }

    @GetMapping("fileCate/add")
    public String add(Model m) {
        m.addAttribute("cates", fcdao.queryAllCate());
        return "/admin/file_cate/add";
    }

    @PostMapping("fileCate/asave")
    @ResponseBody
    public Map<String, Object> asave(FileCate fc) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (fcdao.save(fc) == 1) {
            map.put("code", 1);
            map.put("msg", "添加分类成功");
        } else {
            map.put("code", 0);
            map.put("msg", "添加分类失败");
        }
        return map;
    }

    @PostMapping("fileCate/del/{id}")
    @ResponseBody
    public Map<String, Object> del(@PathVariable("id") int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        if(fcdao.existSubCateById(id)==0){
            if (fcdao.delById(id) == 1) {
                map.put("code", 1);
                map.put("msg", "删除分类成功");
            } else {
                map.put("code", 0);
                map.put("msg", "删除分类失败");
            }
        }else{
            map.put("code", 0);
            map.put("msg", "删除分类失败,请选择删除子类。");
        }
        return map;
    }

    @GetMapping("fileCate/edit")
    public String edit(int id, Model m) {
        m.addAttribute("cate", fcdao.queryById(id));
        m.addAttribute("cates", fcdao.queryAllCate());
        return "/admin/file_cate/edit";
    }

    @PostMapping("fileCate/esave")
    @ResponseBody
    public Map<String, Object> esave(FileCate fc) {
        System.out.println("map*************************************");
        FileCate curr = fcdao.queryById(fc.getId());
        if (fc.getPid() == 0) {
            curr.setPid(0);
            curr.setPath(",0");
            curr.setLevel(1);
        } else {
            FileCate parent = fcdao.queryParentById(fc.getPid());

            System.out.println(parent);
            curr.setPid(fc.getPid());
            curr.setPath(parent.getPath() + "," + parent.getId());
            curr.setLevel(parent.getLevel() + 1);
        }
        curr.setName(fc.getName());
        curr.setSort(fc.getSort());
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(curr);

        if (fcdao.update(curr) == 1) {
            map.put("msg", "修改成功");
            map.put("code", 1);
        } else {
            map.put("msg", "修改错误");
            map.put("code", 0);
        }
        return map;
    }
}